/* cmm.c --- 
 * 
 * @file cmm.c
 * @brief Communication Management Module
 * @author Molnár Zoltán
 * @date Sat Jan 25 12:35:52 2014 (+0100)
 * Version: 
 * Last-Updated: Sun Feb  2 21:42:42 2014 (+0100)
 *           By: Molnár Zoltán
 * 
 */

/* This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth
 * Floor, Boston, MA 02110-1301, USA.
 */

/*******************************************************************************/
/* INCLUDES                                                                    */
/*******************************************************************************/
#include "common.h"
#include "string.h"
#include "util.h"
#include "cli.h"

/*******************************************************************************/
/* DEFINED CONSTANTS                                                           */
/*******************************************************************************/


/*******************************************************************************/
/* MACRO DEFINITIONS                                                           */
/*******************************************************************************/


/*******************************************************************************/
/* TYPE DEFINITIONS                                                            */
/*******************************************************************************/


/*******************************************************************************/
/* DEFINITIONS OF GLOBAL CONSTANTS AND VARIABLES                               */
/*******************************************************************************/


/*******************************************************************************/
/* DEFINITION OF LOCAL FUNCTIONS                                               */
/*******************************************************************************/
/**
 * Calculate checksum
 */
uint32_t LOC_u32AppendChecksum( unsigned char *pucStr, uint32_t u32StrLen)
{
  uint32_t u32Crc = 0;
  uint32_t u32i = 0;
  
  for( u32i = 1; u32i < (u32StrLen) - 1; u32i++)
    u32Crc ^= pucStr[u32i];
  
  return UTL_u32ItoA( pucStr + u32StrLen, u32Crc);
}

/*******************************************************************************/
/* DEFINITION OF GLOBAL FUNCTIONS                                              */
/*******************************************************************************/
/**
 * Communication Management Module task function
 */
void vTaskCmm( void *pvParameters)
{
  unsigned char pucMsg[70];
  uint32_t u32StrLen = 0;

  for(;;)
    {
      LK8EX1_t *ptstrData;

      /* Wait message that contains calculated data */
      xQueueReceive( MeasDataQueue, (void *)&ptstrData, portMAX_DELAY);

      /* Format specifier */
      strcpy( pucMsg, "$LK8EX1,");
      u32StrLen = strlen( pucMsg);
      
      /* Raw pressure */
      u32StrLen += UTL_u32ItoA(pucMsg + u32StrLen, ptstrData->u32Pressure);
      strcat( pucMsg, ",");
      u32StrLen = strlen( pucMsg);

      /* Altitude in meters */
      u32StrLen += UTL_u32ItoA(pucMsg + u32StrLen, ptstrData->u32Altitude);
      strcat( pucMsg, ",");
      u32StrLen = strlen( pucMsg);

      /* Vario in cm/s */
      u32StrLen += UTL_u32ItoA(pucMsg + u32StrLen, ptstrData->u32Vario);
      strcat( pucMsg, ",");
      u32StrLen = strlen( pucMsg);
      
      /* Temperature in C */
      u32StrLen += UTL_u32ItoA(pucMsg + u32StrLen, ptstrData->u32Temp);
      strcat( pucMsg, ",");
      u32StrLen = strlen( pucMsg);
      
      /* Battery */
      u32StrLen += UTL_u32ItoA(pucMsg + u32StrLen, ptstrData->u32Battery);
      strcat( pucMsg, "*");
      u32StrLen = strlen( pucMsg);
      
      /* Calculate checksum */
      u32StrLen += LOC_u32AppendChecksum( pucMsg, u32StrLen);
      
      strcat( pucMsg, "\n\r");


      /* Send string via serial */
      CLI_vidSendString( pucMsg, strlen(pucMsg));

    }
}
      
/******************************* END OF FILE ***********************************/
      
      
